using System;

namespace CodingWheels.DataTypes.Core
{
	internal class UnitSet : IEquatable<UnitSet>
	{
		public Unit FromUnit
		{
			get;
			private set;
		}

		public Unit ToUnit
		{
			get;
			private set;
		}

		public UnitSet(Unit fromUnit, Unit toUnit)
		{
			FromUnit = fromUnit;
			ToUnit = toUnit;
		}

		public UnitSet GetInverse()
		{
			return new UnitSet(ToUnit, FromUnit);
		}

		public bool Equals(UnitSet other)
		{
			if(ReferenceEquals(null, other))
			{
				return false;
			}

			return ReferenceEquals(this, other) || 
			       (FromUnit == other.FromUnit && ToUnit == other.ToUnit) ||
			       (FromUnit == other.ToUnit && ToUnit == other.FromUnit);
		}

		public override bool Equals(object obj)
		{
			if(ReferenceEquals(null, obj))
			{
				return false;
			}

			return obj.GetType() == typeof(UnitSet) && Equals((UnitSet)obj);
		}

		public override int GetHashCode()
		{
			return ToUnit.GetHashCode() * 123 + FromUnit.GetHashCode() * 123;
		}

		public static bool operator ==(UnitSet left, UnitSet right)
		{
			return !ReferenceEquals(null, left) && left.Equals(right);
		}

		public static bool operator !=(UnitSet left, UnitSet right)
		{
			return !(left == right);
		}
	}
}